<?php
declare (strict_types = 1);
/**
 * 素材模型
 */
namespace app\common\model;

class PgmInfoPlay extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_pgm_info_play';

    public function channelHasOne()
    {
        return $this->hasOne(ChannelInfo::class, 'id', 'channel_id');
    }

    public function pgmplayList($where = [], $offset = 1, $length = 50, $ordername="play_id", $order="asc")
    {
        $field = '*';
        $whereMat = [];

        $list = self::with([
            'channelHasOne' => function ($query) use ($whereMat) {
                $query->withField(['id','channel_name'])->hidden(['id'])->where($whereMat);
            }
        ])->field($field)->where($where)->limit(($offset) * $length, $length)->order([$ordername => $order])->select()->toArray();

        foreach($list as $k =>$v) {
            if($v['channelHasOne'] != null) {
                $list[$k]['channel_name'] = $v['channelHasOne']['channel_name'];                               
            } else {
                $list[$k]['channel_name'] = null;
            }
            unset($list[$k]['channelHasOne']);
        }  

        $count = self::where($where)->count('pgm_id');

        return [
            'list'  => $list,
            'total' => $count,
            'pagenum' => $offset
        ];        
    }
}
